"use strict";

/**
 * @ngdoc directive
 * @name jgappApp.directive:FileUpload
 * @description
 * # FileUpload
 */
angular
  .module("jgappApp")
  .directive("fileUpload", [
    "Util",
    function (Util) {
      return {
        scope: {
          fileData: "=fileData",
          handleChange: "=handleChange"
        },
        replace: true,
        template:
          "<div>" +
          '<div class="list-item">' +
          '<div class="item-label">附件</div>' +
          '<div class="flow-content tr" style="padding-right:12px">' +
          '{{holder}}' +
          "</div>" +
          "</div>" +
          '<div class="file-upload" file-list file-data="fileData" handle-change="handleChange"></div>' +
          '<div class="addpic-btn" ng-click="addpic()"></div>' +
          "</div>",
        restrict: "AE",
        link: function postLink(scope, element, attrs) {
          scope.maxnum = attrs.maxnum || 16; //审批页面则不显示
          scope.holder = attrs.holder || "总数不超过" + scope.maxnum + "个"; //审批页面则不显示删除按钮
          scope.addpic = function () {
            var fileArr = (scope.fileData && scope.fileData.split("|")) || [];
            if (fileArr.length < scope.maxnum) {
              var num = scope.maxnum - fileArr.length;
              //
              var type = parseInt(attrs.type) || 1;
              var param = {
                num: num,
                type: type
              };
              console.log(type);
              NativeUtil.use(
                "choosePhoto",
                function (data) {
                  if (scope.fileData) {
                    scope.fileData = scope.fileData + "|" + data;
                  } else {
                    scope.fileData = data;
                  }
                  scope.$apply();
                  if (scope.handleChange) {
                    scope.handleChange()
                  }
                },
                param
              );
            } else {
              Util.Toast("只能上传" + scope.maxnum + "个附件");
              scope.maxlength = true;
              return;
            }
          };
        }
      };
    }
  ])
  .directive("fileList", [
    "Util",
    "FileService",
    "$filter",
    "OpenFile",
    function (Util, FileService, $filter, OpenFile) {
      return {
        scope: {
          fileData: "=fileData",
          handleChange: "=handleChange"
        },
        replace: true,
        template:
          '<div ng-show="fileData">' +
          '<div class="file-label">附件</div>' +
          '<ul class="file-list cl" ng-hide="!fileData">' +
          '<li ng-repeat="item in fileArr track by $index" ng-class="{img:isImg(item)}">' +
          '<img ng-click="goDetail(item)" ng-src="{{item|showimg}}" ng-if="item|ifimg" />' +
          '<span class="c-blue" ng-click="goDetail(item)" ng-hide="item|ifimg" style="flex:1">{{item|fileName}}</span>' +
          '<span class="c-red" ng-click="delpic(item,$index)" ng-if="!checked" ng-class="{icon_del1:isImg(item)}" style="min-width:32px">删除</span>' +
          '<span class="c-blue fr" ng-click="download(item,$index)" ng-if="item|iffile" >下载</span>' +
          "</li>" +
          "</ul>" +
          "</div>",
        restrict: "AE",
        link: function postLink(scope, element, attrs) {
          scope.checked = !!attrs.checked; //审批页面则不显示删除按钮
          scope.fileArr = []; //审批页面则不显示
          scope.$watch("fileData", function (newValue, oldValue) {
            if (newValue) {
              var allfiles = newValue.split("|"); //图片数组
              var imgs = [], files = [];
              allfiles.forEach(function (file) {
                if (file) {
                  if ($filter("ifimg")(file)) {
                    imgs.push(file);
                  } else {
                    files.push(file);
                  }
                }
              })
              scope.fileArr = imgs.concat(files)
            } else {
              scope.fileArr = [];
            }
          });
          /**
           * 删除制定附件
           * @param  {[type]} item  [附件信息]
           * @param  {[type]} index [当前附件位置]
           * @return [type]         [description]
           */
          var delPic = function (item, index) {
            return function () {
              FileService.delfile(item).then(function (data) {
                Util.Toast(data);
                scope.fileArr.splice(index, 1);
                scope.fileData = scope.fileArr.join("|");
                if (scope.handleChange) {
                  scope.handleChange()
                }
              });
            };
          };
          scope.delpic = function (item, index) {
            navigator.notification.confirm(
              "确定删除附件",
              delPic(item, index),
              "提示",
              ["确定", "取消"]
            );
          };
          scope.isFile = Util.isFile;
          scope.isImg = Util.isImg;
          /**
           * 查看图片详情
           * @param  {[type]} fileData [文件]
           * @return [type]            [description]
           */
          scope.goDetail = function (fileData) {
            // 如果点击的是图片则过滤出所有图片预览
            if (Util.isImg(fileData)) {
              var imgarr = scope.fileArr.filter(function (item, index) {
                return Util.isImg(item);
              });

              var index = imgarr.indexOf(fileData);
              var param = {};
              imgarr = imgarr.map(function (item) {
                return encodeURIComponent(item);
              });
              param.data = imgarr.join("|");
              param.index = index;
              param.showbtn = true;
              NativeUtil.use("photoPreview", null, param);
            } else if (Util.isFile(fileData)) {
              OpenFile.open(fileData);
            } else {
              Util.Toast("移动端不支持该附件的预览");
            }
          };
          scope.download = function (fileData) {
            OpenFile.downLoadFile(fileData);
          };
        }
      };
    }
  ]);
